package com.cpit.platform.web.controller.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cpit.platform.common.entity.Result;
import com.cpit.platform.common.exception.CodeException;
import com.cpit.platform.common.utils.ResultUtil;

@ControllerAdvice
public class ControllerExceptionHandle {

	private final static Logger logger = LoggerFactory.getLogger(ControllerExceptionHandle.class);

	@ResponseBody
	@ExceptionHandler(value = Exception.class)
	public Result handler(Exception e) {

		if (e instanceof CodeException) {
			CodeException codeException = (CodeException) e;
			return ResultUtil.error(codeException.getCode(), e.getMessage());
		} else if(e instanceof AccessDeniedException) {
			logger.error("AccessDeniedException exception : ", e);
			return ResultUtil.error("2", "权限异常");
		} else {
			logger.error("system exception : {}", e);
			return ResultUtil.error("-1", "未知错误");
		}
	}
}
